package com.qzsoft.zhjd.asphalt.modules.producemonitor.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.qzsoft.zhjd.asphalt.modules.asphaltdata.entity.AsphaltDataEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltdata.service.AsphaltDataService;
import com.qzsoft.zhjd.asphalt.modules.asphaltdatasieve.entity.AsphaltDataSieveEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltdatasieve.service.AsphaltDataSieveService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchThresholdEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchTypeEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchHotService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchThresholdService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchTypeService;
import com.qzsoft.zhjd.asphalt.modules.producemonitor.dto.ProduceMonitorChartDto;
import com.qzsoft.zhjd.asphalt.modules.producemonitor.dto.ProduceMonitorDayChartDto;
import com.qzsoft.zhjd.asphalt.modules.producemonitor.service.AsphaltProduceMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;

/**
 * @ClassName AsphaltProduceMonitorServiceImpl
 * @Description 沥青生产监控业务接口实现
 * @author linhuawei
 * @since 2022-01-29
 */
@Service
public class AsphaltProduceMonitorServiceImpl implements AsphaltProduceMonitorService {

    @Autowired
    private AsphaltDataService asphaltDataService;

    @Autowired
    private AsphaltDataSieveService asphaltDataSieveService;

    @Autowired
    private AsphaltMatchService asphaltMatchService;

    @Autowired
    private AsphaltMatchTypeService asphaltMatchTypeService;

    @Autowired
    private AsphaltMatchThresholdService asphaltMatchThresholdService;

    @Autowired
    private SysStationService sysStationService;

    @Autowired
    private AsphaltMatchHotService asphaltMatchHotService;

    @Override
    public ProduceMonitorChartDto getChartData(String id) {
        // 获取生产数据
        AsphaltDataEntity data = asphaltDataService.getById(id);
        if (null == data) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "生产数据不存在");
        }

        // 需要返回的结果数据
        ProduceMonitorChartDto dto = new ProduceMonitorChartDto();
        dto.setData(data);

        // 获取筛分数据
        AsphaltDataSieveEntity sieveData = asphaltDataSieveService.getById(id);
        dto.setSieveData(sieveData);

        // 获取设计生产配置比
        AsphaltMatchEntity match = asphaltMatchService.getByCode(data.getMatchCode());
        dto.setMatch(match);

        // 获取上下限
        AsphaltMatchThresholdEntity matchThreshold = asphaltMatchThresholdService.getByMatchName(data.getMatchName());
        dto.setMatchThreshold(matchThreshold);

        // 获取级配类型
        AsphaltMatchTypeEntity matchType = asphaltMatchTypeService.getByType(data.getMatchType());
        dto.setMatchType(matchType);

        // 返回结果数据
        return dto;
    }

    @Override
    public ProduceMonitorDayChartDto getDayChartData(String stationCode, String matchCode, String day) {
        // 返回结果
        ProduceMonitorDayChartDto dto = new ProduceMonitorDayChartDto();

        // 获取配方编号
        AsphaltMatchEntity match = asphaltMatchService.getByCode(matchCode);
        if (null != match) {
            // 获取上下限
            AsphaltMatchThresholdEntity matchThreshold = asphaltMatchThresholdService
                    .getByMatchName(match.getMatchName());
            dto.setMatchThreshold(matchThreshold);

            // 获取级配类型
            AsphaltMatchTypeEntity matchType = asphaltMatchTypeService.getByType(match.getMatchType());
            dto.setMatchType(matchType);

            // 获取热料配置信息
            Map<String, BigDecimal> targetMap = asphaltMatchHotService.getTargetMap(match.getId());
            dto.setTargetMap(targetMap);

            stationCode = match.getStationCode();
        }

        // 获取拌合楼信息
        SysStationEntity station = null;
        if (ObjectUtils.isNotEmpty(stationCode)) {
            station = sysStationService.getStationByCode(stationCode);
        }
        dto.setStation(station);

        // 获取筛分数据
        List<AsphaltDataSieveEntity> sieveDataList = asphaltDataSieveService.listForDay(stationCode, matchCode, day);
        dto.setSieveDataList(sieveDataList);

        return dto;
    }

}
